﻿@using System.Reflection
@using XCode
@using XCode.Configuration
@{
    Layout = null;
    //2021年9月30日 照月(zshankang)修改，在非ACE主题下，直接使用 Model.Role 会报错。
    Role role = null;
    if (Model.GetType().GetProperties().Length > 0)
    {
        role = ((System.Reflection.PropertyInfo[])Model.GetType().GetProperties())[0].GetValue(Model, null);
    }
    //var role = Model.Role as Role;
    var menus = Menu.Root.AllChilds;
    //var pfs = EnumHelper.GetDescriptions<PermissionFlags>().Where(e => e.Key > PermissionFlags.None);
}
<script type="text/javascript">
    $(function () {
        $('input.authorize').on('change', function () {
            var $this = $(this);

            if ($this.attr("childchange") == 0) {
                var status = $this.prop('checked');
                var childkey = $this.attr('child');
                // 规避change 需要在失去焦点时触发的问题，设置值完成后手工再次触发该事件
                $('input[parentkey="' + childkey + '"]').prop('checked', status).change();

                var parentkey = $this.attr('parentkey');

                var obj = $("input[child$='" + parentkey + "']");
                var obj1 = $("input[parentkey$='" + parentkey + "']");;
                var bol = false;
                for(i = 0; i < obj1.length; i++){
                    if ($('#' + obj1[i].id).prop("checked")) {
                        bol = true;
                        continue;
                    }
                }
                if (!bol) {
                    $("#" + obj.attr("id")).prop("checked","");
                }
            }
            else if ($this.attr("childchange") == 1){
                var parentkey = $this.attr('parentkey');
                var obj = $("input[child$='" + parentkey + "']");
                var parentkey1 = $("#" + obj.attr("id")).attr('parentkey');
                var obj1 = $("input[child$='" + parentkey1 + "']");

                $("#" + obj.attr("id")).prop('checked', true);
                $("#" + obj1.attr("id")).prop('checked', true);

                $this.attr("childchange", 0);
            }
            else if ($this.attr("childchange") == 2) {
                var parentkey = $this.attr('parentkey');
                var obj = $("input[child$='" + parentkey + "']");

                var parentkey1 = $("#" + obj.attr("id")).attr('parentkey');
                var obj1 = $("input[child$='" + parentkey1 + "']");

                var obj1 = $("input[parentkey$='" + parentkey + "']");;
                var bol = false;
                for(i = 0; i < obj1.length; i++){
                    if ($('#' + obj1[i].id).prop("checked")) {
                        bol = true;
                        continue;
                    }
                }
                if (!bol) {
                    $("#" + obj.attr("id")).attr("childchange", 3);
                    $("#" + obj.attr("id")).prop("checked", "").change();
                }

                $this.attr("childchange", 0);
            }
            else if ($this.attr("childchange") == 3) {
                var parentkey = $this.attr('parentkey');
                var obj = $("input[child$='" + parentkey + "']");

                $("#" + obj.attr("id")).prop('checked', "");

                $this.attr("childchange", 0);
            }
        });
        // 只读/查看
        $('input.pro_detail').on('change', function () {
            $this = $(this);
            var status = $this.prop('checked');
            var key = $this.attr('prochildkey');
            $('input[proparentkey=' + key + ']').prop('checked', status);
        });
        // 全部权限
        $('input.pro_all').on('change', function () {
            $this = $(this);
            var status = $this.prop('checked');
            var key = $this.attr('prochildkey');
            $('input[prokey=' + key + ']').prop('checked', status);
        });

        $('td.childpf input').on('change', function() {
            var dataid = $('#' + this.id).attr("dataid");

            if ($('#' + this.id).prop("checked")){
                $("#p" + dataid).attr("childchange", 1);
                $("#p" + dataid).prop("checked","checked").change();
            }
            else {
                var obj = $('#' + this.id).parent().find("input");
                var bol = false;
                for(i = 0; i < obj.length; i++){
                    if ($('#' + obj[i].id).prop("checked")) {
                        bol = true;
                        continue;
                    }
                }

                if (!bol) {
                    $("#p" + dataid).attr("childchange", 2);
                    $("#p" + dataid).prop("checked", "").change();
                }
            }
        });
    });
</script>
<div class="form-group col-md-12">
    <label class="control-label col-md-2">授权</label>
    <div class="input-group col-md-10">
        <table class="table table-bordered table-hover table-striped table-condensed">
            <thead>
                <tr>
                    <th>名称</th>
                    <th>显示名</th>
                    <th>授权</th>
                    <th>操作权限</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var entity in menus)
                {
                    <tr>
                        <td>@entity.TreeNodeName</td>
                        <td>@entity.DisplayName</td>
                        <td>@Html.CheckBox("p" + entity.ID, role.Has(entity.ID), new { @class = "authorize", @child = "auth_child" + entity.ID, @parentkey = "auth_child" + entity.ParentID, @childchange = 0 })</td>
                        <td class="childpf">
                            @if (entity.Childs.Count == 0)
                            {
                                foreach (var item in entity.Permissions.OrderBy(e => e.Key))
                                {
                                    var id = "pf" + entity.ID + "_" + ((Int32)item.Key);
                                    @Html.CheckBox(id, role.Has(entity.ID, (PermissionFlags)item.Key), new { @parentkey = "auth_child" + entity.ID, @proparentkey = "pro_" + item.Key + "_" + entity.ParentID, @prokey = "pro_" + (UInt32)PermissionFlags.All + "_" + entity.ParentID, @dataid = entity.ID })
                                    @Html.Label(id, item.Value)
                                    <text>&nbsp;</text>
                                }
                            }
                            else
                            {

                                @Html.CheckBox("pc_readonly_" + entity.ID, false, new { @class = "pro_detail", @prochildkey = "pro_" + (UInt32)PermissionFlags.Detail + "_" + entity.ID })
                                @Html.Label("pc_readonly_" + entity.ID, "只读")
                                <text>&nbsp;</text>
                                @Html.CheckBox("pc_all_" + entity.ID, false, new { @class = "pro_all", @prochildkey = "pro_" + (UInt32)PermissionFlags.All + "_" + entity.ID })
                                @Html.Label("pc_all_" + entity.ID, "全部")
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>